package com.leetcode.no621;

public class Solution {
    public int leastInterval(char[] tasks, int n) {
        int len = tasks.length;
        int res = 0;
        if(n == 0){
            return len;
        }

        // 统计哪个字母出现得最多
        int[] charCount=new int[26];
        for(char task:tasks){
            charCount[task-'A'] += 1;
        }

        // 出现最多的任务数
        int max = 0;

        // 出现最多的任务同时有几个
        int maxCount = 0;
        for(int num:charCount){
            if(num>max){
                max=num;
                maxCount =1;
            }else if(num == max){
                ++maxCount;
            }
        }

        res = Math.max(len, (max-1)*(n+1) + maxCount);


        return res;
    }
}
